<?php 

require_once( 'Database.php' );

class User {

  var $userid;
  var $username;
  var $password;
  var $email;
  var $lastlogin;
  var $admin;

  function User( $_data ) {

    $this->userid = $_data->userid;
    $this->username = $_data->username;
    $this->password = $_data->password;
    $this->email = $_data->email;
    $this->lastlogin = $_data->lastlogin;
    $this->admin = $_data->admin;

  }

  function getAllUsers() {

    $users = array();
    $qry = "SELECT * FROM user";
    $result = query( $qry );

    while( $row = mysql_fetch_object( $result ) ) {
      array_push( $users, $row );
    }

    return $users;

  }

  function addUser( $_username, $_password, $_email, $_admin ) {

    $date = date( 'Y-m-d' );
    $qry = "INSERT INTO user VALUES ( '', '$_username', '$_password', '$_email', '$date', '$_admin');"; 
    query( $qry );

  }

  function deleteUser( $_userid ) {

    $qry = "DELETE FROM user WHERE userid='$_userid';";
    query( $qry );

  }

  function authenticateUser( $_username, $_password ) {

    $qry = "SELECT username, password FROM user WHERE username='$_username';";

    $result = query( $qry );
    if( $result ) {
      $row = mysql_fetch_object( $result );
    }

    if( $_password == $row->password ) {
      return true;
    } else {
      return false;
    }

  }

  function getUserByID( $_id ) {

    $qry = "SELECT * FROM user WHERE userid='$_id';";

    $result = query( $qry );
    if( $result ) {
      $row = mysql_fetch_object( $result );
    }

    $user = new User( $row );
    return $user;

  }

  function getUserByUsername( $_username ) {
    
    $qry = "SELECT * FROM user WHERE username='$_username';";

    $result = query( $qry );
    if( $result ) {
      $row = mysql_fetch_object( $result );
    }

    $user = new User( $row );
    return $user;

  }

}

?>